package com.icarcom.cn.util.printUtil;

import android.content.Context;

import com.example.print_sdk.EscUtils;
import com.example.print_sdk.SerialManager;
import com.example.print_sdk.enums.ALIGN_MODE;
import com.example.print_sdk.enums.BARCODE_1D_TYPE;

/**
 * Created by moxiaomo
 * on 2020/4/1
 */
public class PrintContract {

    private Context mContext;
    private SerialManager mSerialManager;
    private String TAG=PrintContract.class.getName ();


    public PrintContract() {
    }

    public PrintContract(Context context, SerialManager serialManager) {
        this.mContext=context;
        this.mSerialManager=serialManager;
    }

    public void set_number(int number) {
        byte[] bytes=ByteUtils.little_intToByte (number);
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_init_print ();
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void printInit() {
        EscUtils escCmd=new EscUtils(1024);
        escCmd.esc_init_print (); // init print
        escCmd.esc_auto_enable_mark (false);
        escCmd.esc_no_paper_print_status ((byte) 0);
        escCmd.esc_select_sensor_stop_print ();
        escCmd.esc_set_encode ((byte) 2);
//        escCmd.esc_set_language ((byte) 15);
//        escCmd.esc_set_speed ((byte) 200);
//        escCmd.esc_set_mark_distance_print (130);
        mSerialManager.escCommand (escCmd.getEscBuffer ());
        try {
            Thread.sleep (10);
        } catch (InterruptedException e) {
            e.printStackTrace ();
        }
    }


    /**
     * print text
     */
    public void printText(int number,String content) {
        byte[] bytes=ByteUtils.little_intToByte (number);
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_init_print ();
//        escCmd.esc_enter_print ();
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        escCmd.esc_set_concentration (30);
        escCmd.esc_text_print (content);
        escCmd.esc_text_print ("\n");
        escCmd.esc_print_align (ALIGN_MODE.ALIGN_LEFT);
        escCmd.esc_set_sn_end (); // 结束一票一控 End one vote and one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void printQR(int number,String url) {
        byte[] bytes=ByteUtils.little_intToByte (number);
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        //   escCmd.esc_set_black (48, 00); // 退纸 Eject paper
        escCmd.esc_barcode_2D_print ((byte) 7, (byte) 14, (byte) 49, ALIGN_MODE.ALIGN_CENTER, url+"\n");
        escCmd.esc_text_print ("\n");
        escCmd.esc_set_sn_end (); // 结束一票一控 End one vote and one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void printQRText(int number,String content,String url){
        byte[] bytes=ByteUtils.little_intToByte (number);
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_print_align (ALIGN_MODE.ALIGN_LEFT);
        escCmd.esc_init_print ();
//        escCmd.esc_enter_print ();
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        escCmd.esc_set_concentration (30);
        escCmd.esc_text_print (content);
        escCmd.esc_barcode_2D_print ((byte) 6, (byte) 12, (byte) 49, ALIGN_MODE.ALIGN_CENTER, url+"\n");
        escCmd.esc_text_print ("\n");
        escCmd.esc_text_print ("\n");
        escCmd.esc_text_print ("\n");
        escCmd.esc_set_sn_end (); // 结束一票一控 End one vote and one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void printBarCode(int number) {
        byte[] bytes=ByteUtils.little_intToByte (number);
        String upca="987654321012";
        String upce="987654321012";
        String ena13="1234567890123";
        String ena8="1234567";
        String code39="123456";
        String code93="12345678ABcd%";
        String code128="12345%$()ABcdq";


        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_init_print ();
        escCmd.esc_enter_print ();
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        escCmd.esc_set_speed ((byte) 220);
        escCmd.esc_print_align (ALIGN_MODE.ALIGN_CENTER);


        escCmd.esc_text_print ("-------------UPCA-------------");
        escCmd.esc_text_print ("\t");
        escCmd.esc_barcode_height ((byte) 80);
        escCmd.esc_barcode_width ((byte) 2);
        escCmd.esc_barcode_1D_print (upca, BARCODE_1D_TYPE.UPCA);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------UPCE--------------\r\n");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_1D_print (upce, BARCODE_1D_TYPE.UPCE);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------ENA8--------------\r\n");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_1D_print (ena8, BARCODE_1D_TYPE.EAN8);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------ENA13--------------\r\n");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_1D_print (ena13, BARCODE_1D_TYPE.EAN13);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------CODE39--------------\r");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_height ((byte) 80);
        escCmd.esc_barcode_width ((byte) 2);
        escCmd.esc_barcode_1D_print (code39, BARCODE_1D_TYPE.CODE39);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------CODE93--------------\r");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_height ((byte) 80);
        escCmd.esc_barcode_width ((byte) 2);
        escCmd.esc_barcode_1D_print (code93, BARCODE_1D_TYPE.CODE93);
        escCmd.esc_enter_print ();

        escCmd.esc_print_line_space (false, (byte) 0x48);//Set line spacing
        escCmd.esc_text_print ("-------------CODE128-------------\r");
        escCmd.esc_print_line_space (true, (byte) 0x00);// Set default line spacing
        escCmd.esc_barcode_height ((byte) 80);
        escCmd.esc_barcode_width ((byte) 2);
        escCmd.esc_barcode_1D_print (code128, BARCODE_1D_TYPE.CODE128);
        escCmd.esc_enter_print ();
        escCmd.esc_set_sn_end (); // 结束一票一控 End one vote and one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }


    public void data_print() {
        String DID="J-HZ-190930-5214-01";
        String bydPn="12856400-00";
        String custPn="03026XXQ";
        String wc="5FDB";
        String Qty="100";
        String dc="9D1937";

        EscUtils EscCmd=new EscUtils(2048);
        EscCmd.esc_init_print ();
        EscCmd.esc_enter_print ();

        EscCmd.esc_set_concentration (35);
        EscCmd.esc_print_align (ALIGN_MODE.ALIGN_CENTER);
        EscCmd.esc_enter_print ();
        EscCmd.esc_barcode_height ((byte) 80);
        EscCmd.esc_barcode_width ((byte) 1);
        EscCmd.esc_barcode_1D_print (DID, BARCODE_1D_TYPE.CODE128);
        EscCmd.esc_enter_print ();
        EscCmd.esc_print_align (ALIGN_MODE.ALIGN_LEFT);
        EscCmd.esc_text_print ("DID: " + DID + "\r\n");


        EscCmd.esc_feed_paper (false, (byte) 0);
        mSerialManager.escCommand (EscCmd.getEscBuffer ());
    }


    public void printLabel(int number) {
        byte[] bytes=ByteUtils.little_intToByte (number);
        // cannot access
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_set_sn (bytes); // 开启一票一控 Open one vote one control
        escCmd.esc_set_black (48, 00); // 退纸 Eject paper
        escCmd.esc_barcode_height ((byte) 80);
        escCmd.esc_barcode_width ((byte) 1);
        escCmd.esc_barcode_1D_print ("MFl: 357576032533810", BARCODE_1D_TYPE.CODE128);

//        escCmd.esc_text_print (textEnContent1());
        escCmd.esc_enable_mark (true); // 启用黑标 Enable black mark
        escCmd.esc_main_mark (); // 转到下一个黑色标记 Go to next black mark
        escCmd.esc_set_sn_end (); //  结束一票一控 End one vote and one control
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void psam(int type) {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_psam_info (type);
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void getVersion() {
        EscUtils escCmd=new EscUtils(2048);
        escCmd.esc_init_print ();
        escCmd.esc_lib_version ();
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void checkPaper() {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_print_state ();
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void backPager() {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_set_black (48, 00); // 退纸 Eject paper
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void left_mag(int left) {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_print_left_margin ((short) left);
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void mark() {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_enable_mark (true); // 启用黑标 Enable black mark
        escCmd.esc_main_mark (); // 转到下一个黑色标记 Go to next black mark
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void featureList() {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_feature_list ();
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void check() {
        EscUtils escCmd=new EscUtils(128);
        escCmd.esc_feature_list ();
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

    public void printDefault() {
        EscUtils escCmd=new EscUtils(1024);
        escCmd.esc_default_print (); // default print
        mSerialManager.escCommand (escCmd.getEscBuffer ());
    }

}
